<?php
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php';
/**
 * 从决策系统取存销比数据
 */
class SyncSaleStockPreController extends Controller_CronJob {
	// 分批
	private $limit = 1000;
	public function run() {
		$this->save();
	}
	private function save() {
		$model = new Cron_Model_Decision_SaleStockPre();
		$count = $model->getAccout();
		$page = ceil($count/$this->limit);
		for ($i = 0; $i < $page; $i++) {
			$data = $model->getData($i*$this->limit, $this->limit);
			foreach ($data as $pre) {
				$id = $model->existStockData($pre['goods_sn'], $pre['stock_code']);
				if ($id) {
					try {
						$model->updateStock($pre['sale_stock_pre'], $id);
					}catch (Exception $e) {
						$this->log->record("SKU: ".$pre['sku'].$e->getMessage(), Log::ERR);
					}
				}
			}
		}
		echo "success";
	}
}
$sale_stock_pre = new SyncSaleStockPreController();
$sale_stock_pre->run();